﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Eneta.Portal.Common;
using Eneta.Portal.Common.Repository;

namespace Eneta.Portal.Companies.WebParts.IOMenu
{
    [ToolboxItem(false)]
    public class IOMenu : WebPart
    {
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            try
            {
                //if (Resources.Companies.Culture == null)
                //    Resources.Companies.Culture = Thread.CurrentThread.CurrentUICulture;

                Controls.Add(new LiteralControl("<h3><a>" + Resources.GetResourceString("Classifications") + "</a></h3>"));

                var repository = new CompanyRepository(EnetaApplication.PortalCompaniesSiteUrl);
                var levels = repository.GetVisibleIoLevels();

                if (levels == null)
                {
                    Controls.Add(new LiteralControl("&nbsp;"));
                    return;
                }
                if (levels.Count == 0)
                {
                    Controls.Add(new LiteralControl("&nbsp;"));
                    return;
                }

                int l1, l3;

                int.TryParse(HttpContext.Current.Request["l1"], out l1);
                int.TryParse(HttpContext.Current.Request["l3"], out l3);

                AddLiteral("<ul>");

                foreach (var level1 in levels)
                {
                    AddLiteral("<li");

                    if (level1.Id == l1)
                    {
                        AddLiteral(" class=\"active\"");
                    }

                    AddLiteral("><a href=\"default.aspx?l1=");
                    AddLiteral(level1.Id.ToString());
                    AddLiteral("\">");
                    AddLiteral(level1.Title);
                    AddLiteral("</a>");

                    if (level1.Level2Items.Count > 0)
                    {
                        AddLiteral("<ul class=\"subnav\">");

                        foreach (var level2 in level1.Level2Items)
                            foreach (var level3 in level2.Level3Items)
                            {
                                AddLiteral("<li");
                                if (level3.Id == l3)
                                    AddLiteral(" class=\"active\"");
                                AddLiteral("><a href=\"default.aspx?l3=");
                                AddLiteral(level3.Id + "\">");
                                AddLiteral(level3.Title);
                                AddLiteral("</a></li>");
                            }

                        AddLiteral("</ul>");
                    }

                    AddLiteral("</li>");
                }

                AddLiteral("</ul>");
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
        }

        private void AddLiteral(string text)
        {
            Controls.Add(new LiteralControl(text));
        }

    }
}
